/*
 * Copyright (c) 2006-2021, Halo team
 *
 * SPDX-License-Identifier: GPLv3
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-09-11     Eywen        the first version
 */
#ifndef APPLICATIONS_DRIVERS_DRV_TEMPRATURE_H_
#define APPLICATIONS_DRIVERS_DRV_TEMPRATURE_H_

#include <rtthread.h>
#include "common.h"

typedef enum temperature_sensor{
    temperature_type_ptc,               /* 正温度系数的热敏电阻 */
    temperature_type_ntc,               /* 负温度系数的热敏电阻 */
    temperature_type_ptd,
}temperature_type_t;

typedef enum {
    temperature_st_uninit,                 /* 未初始化 */
    temperature_st_normal,                 /* 正常 */
    temperature_st_abnormal,               /* 传感器异常 */
} temperature_sensor_status_t;


#define NTC_ZM1460


typedef enum {
#ifdef NTC_ZM1460
    ntc_mode_zm_1460,
#endif
    ntc_mode_max,
}temperature_ntc_mode_t;

/* exp: -23.5 * exp( 0.002771 * x) + 54.92 * exp(-0.0281 * x) */
typedef struct ntc_rt_fitting_para{        /* ntc 温度与电阻拟合参数 */
    double a, b, c, d;
}ntc_rt_fitting_t;

typedef struct _temperature_ntc_ops temperature_ntc_ops_t;

typedef struct _temperature_ntc{
    const char *adc_dev_name;               /* ADC驱动名字 */
    uint32_t  adc_dev_channel;              /* ADC 通带 */
    rt_adc_device_t adc_dev;                /* ADC设备描述符 */
    uint32_t adc_max;                       /* ADC的最大值 */

    temperature_ntc_mode_t mode;            /* 传感器型号 */
    uint32_t resistance_pullup;             /* 上拉电阻, 目前ntc只考虑上拉电阻接法 */
    //uint32_t resistance_pulldowm;         /* 下拉电阻 */

    temperature_sensor_status_t status;     /* 传感器工作状态 */
    const ntc_rt_fitting_t *filtting_para;

    const temperature_ntc_ops_t *ops;              /* 操作接口 */
}temperature_ntc_t;

/* ntc温度传感器操作接口 */
typedef struct _temperature_ntc_ops{
    bool  (*init)(temperature_ntc_t *ntc, const char *adc_dev_name,  uint32_t adc_channle, uint8_t adc_bits,
                  temperature_ntc_mode_t mode, uint32_t resistance_up);
    float (*temperature)(temperature_ntc_t *device);
}temperature_ntc_ops_t;

const void *find_temerature_ops_by_mode(temperature_type_t type);

#endif /* APPLICATIONS_DRIVERS_DRV_TEMPRATURE_H_ */
